package com.pkk.geer.rose.common.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.pkk.geer.rose.common.constant.RoseSpeendParamConstant;
import com.pkk.geer.rose.common.enums.RoseSpeenReqTypeEnum;
import com.pkk.geer.rose.common.vo.RoseSpeendAppHttpParamVo;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.logging.log4j.message.StringFormattedMessage;

/**
 * @description: 极速数据的工具类
 * @author: peikunkun
 * @create: 2019-11-09 17:33
 **/
public class RoseSpeedAppUtil {

  /**
   * @Description: 解析极速数据的请求参数
   * @Param: [reqType]
   * @return: com.pkk.geer.rose.common.vo.RoseSpeendAppHttpParamVo
   * @Author: peikunkun
   * @Date: 2019/11/9 0009 下午 5:41
   */
  public static RoseSpeendAppHttpParamVo parseHttReq(RoseSpeenReqTypeEnum reqType, String reqParam, String reqValue,
      String appKey) {
    String finalReqValue = "";
    final String[] split = handleParseReqValue(reqValue, appKey);
    if (RoseSpeenReqTypeEnum.POST.equals(reqType)) {
      finalReqValue = String.format(reqParam, split);
    } else if (RoseSpeenReqTypeEnum.GET.equals(reqType)) {
      finalReqValue = String.format(reqParam, split);
    } else {
      throw new UnsupportedOperationException("该请求方式不支持");
    }
    return new RoseSpeendAppHttpParamVo(finalReqValue);
  }

  /**
   * @Description: 处理请求值
   * @Param: [reqValue]
   * @return: java.lang.String[]
   * @Author: peikunkun
   * @Date: 2019/11/9 0009 下午 6:05
   */
  private static String[] handleParseReqValue(String reqValue, String appKey) {
    final String[] split = reqValue.split(",");
    AtomicInteger atomicInteger = new AtomicInteger(0);
    Arrays.stream(split).forEach(s -> {
      if (DatePattern.NORM_DATE_PATTERN.equals(s)) {
        split[atomicInteger.get()] = DateUtil.format(new Date(), s);
      }
      if (RoseSpeendParamConstant.ROSE_SPEED_APP_KEY.equals(s)) {
        split[atomicInteger.get()] = appKey;
      }
      atomicInteger.incrementAndGet();
    });
    return split;
  }
}
